package com.whacos.modules.system.service.impl;

import com.whacos.common.utils.GeneratorCodeUtils;
import com.whacos.modules.system.dao.GeneratorDao;
import com.whacos.modules.system.service.GeneratorService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * @author xiongdun
 * @description GeneratorServiceImpl
 * @since 2018/11/17 17:11
 */
@Service
public class GeneratorServiceImpl implements GeneratorService {

    @Autowired
    private GeneratorDao generatorDao;

    @Override
    public List<Map<String, Object>> list() {
        return this.generatorDao.list();
    }

    @Override
    public byte[] generatorCode(String[] tableNames) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for (String tableName : tableNames) {
            //查询表信息
            Map<String, String> table = generatorDao.get(tableName);
            //查询列信息
            List<Map<String, String>> columns = generatorDao.listColumns(tableName);
            //生成代码
            GeneratorCodeUtils.generatorCode(table, columns, zip);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

}
